package com.group12uiowaece.bluetoothexerciseapplication;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.ImageView;
import android.widget.Spinner;

public class HomeFragment extends Fragment{
	
	private View view;
	private Chronometer stopwatch;
	private Button startStopButton;
	private Spinner selectMenu;
	private Boolean started;
	private String imageName;
	private ImageView heartIcon;

	@Override public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
	}
	
	@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
        view = inflater.inflate(R.layout.home_fragment, container, false);
        
        selectMenu = (Spinner) view.findViewById(R.id.spinner1);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
		        R.array.exercise_types_array, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		selectMenu.setAdapter(adapter);
		
		stopwatch = (Chronometer) view.findViewById(R.id.stopWatch);
		stopwatch.stop();
		
		startStopButton = (Button) view.findViewById(R.id.startStopWorkoutButton);
		startStopButton.setOnClickListener(buttonListener);
		
		stopwatch.setOnChronometerTickListener(stopwatchListener);
		
		heartIcon = (ImageView) view.findViewById(R.id.heartIcon);
		
		started = false;
		imageName = "logo";
		
		this.setRetainInstance(true);
		
        return view;
	
	}
	
	OnClickListener buttonListener = new OnClickListener() {
        public void onClick(View v) {
        	if(!started) {
        		stopwatch.setBase(SystemClock.elapsedRealtime());
        		stopwatch.start();
            	started = true;
            	startStopButton.setText(R.string.stop_button_label);
            	selectMenu.setClickable(false);
        	} else {
        		stopwatch.stop();
        		started = false;
        		startStopButton.setText(R.string.start_button_label);
        		selectMenu.setClickable(true);
        	}
        }
    };
    
    OnChronometerTickListener stopwatchListener = new OnChronometerTickListener() {

		@Override
		public void onChronometerTick(Chronometer chronometer) {
			if(imageName == "logo") {
				heartIcon.setImageResource(R.drawable.heartnologo);
				imageName = "nologo";
			} else {
				heartIcon.setImageResource(R.drawable.heart);
				imageName = "logo";
			}
			
		}
    	
    	
    };
	
}
